<template>
  <div class="login-container">
    <el-card class="login-card" shadow="hover">
      <div class="title">
        <h2>用户登录</h2>
      </div>
      
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名" 
            prefix-icon="el-icon-user"
            clearable>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码" 
            prefix-icon="el-icon-lock"
            show-password
            @keyup.enter.native="handleLogin">
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            @click="handleLogin" 
            class="login-button">
            登录
          </el-button>
        </el-form-item>
        
        <div class="tips">
          <span>还没有账号？<router-link to="/register">立即注册</router-link></span>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/auth'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, message: '用户名长度必须大于等于4位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度必须大于等于6位', trigger: 'blur' }
        ]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            const response = await login(this.loginForm)
            
            if (response.data && response.data.token) {
              // 存储token和用户信息
              localStorage.setItem('token', response.data.token)
              localStorage.setItem('userInfo', JSON.stringify({
                id: response.data.id,
                username: response.data.username,
                role: response.data.role
              }))
              
              this.$message({
                message: '登录成功',
                type: 'success'
              })
              
              // 跳转到重定向页面或首页
              this.$router.push({ path: this.redirect || '/' })
            } else {
              this.$message.error('登录失败：未获取到有效的token')
            }
          } catch (error) {
            console.error('登录失败', error)
            this.$message.error(`登录失败：${error.message || '未知错误'}`)
          } finally {
            this.loading = false
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  background-image: linear-gradient(to bottom right, #EDF2F7, #E2E8F0);
}

.login-card {
  width: 400px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.login-form {
  padding: 0 20px;
}

.login-button {
  width: 100%;
  border-radius: 4px;
  font-size: 16px;
  height: 44px;
  margin-top: 10px;
}

.tips {
  text-align: center;
  margin-top: 20px;
  color: #606266;
  font-size: 14px;
}

.tips a {
  color: #409EFF;
  text-decoration: none;
  font-weight: bold;
}

.tips a:hover {
  text-decoration: underline;
}

/* 输入框样式优化 */
/deep/ .el-input__inner {
  height: 44px;
  line-height: 44px;
}

/deep/ .el-form-item {
  margin-bottom: 22px;
}
</style> 